using System;
using UnityEngine;

namespace JH
{
    public class RandomColor : MonoBehaviour
    {
        private static MaterialPropertyBlock _bk;
        private static readonly int Color = Shader.PropertyToID("_BaseColor");

        private void Awake()
        {
            OnValidate();
        }

        private void OnValidate()
        {
            foreach (var each in GetComponentsInChildren<MeshRenderer>())
            {
                _bk ??= new MaterialPropertyBlock();
                each.GetPropertyBlock(_bk);
                _bk.SetColor(Color, UnityEngine.Random.ColorHSV());
                each.SetPropertyBlock(_bk);
            }
        }
    }
}